package yygh.user.Service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import yygh.hospital.enums.DictEnum;
import yygh.hospital.model.user.Patient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import yygh.cmn.client.DictFeignClient;
import yygh.user.Service.PatientService;
import yygh.user.mapper.PatientMapper;

import java.util.List;

@Service
public class PatientServiceImp extends ServiceImpl<PatientMapper, Patient>  implements PatientService {

    @Autowired
    PatientMapper patientMapper;

    //注入dictFeignClient，实现微服务的调用
    @Autowired
    DictFeignClient dictFeignClient;

    //Service层，根据用户编号获取该用户下的所有就诊人信息
    @Override
    public List<Patient> findAllByUserId(Long userId) {
        //mybatis-plus条件查询
        QueryWrapper queryWrapper=new QueryWrapper<Patient>();
        queryWrapper.eq("user_id",userId);
        List<Patient> patientList = patientMapper.selectList(queryWrapper);

        //遍历list，获取每一个就诊人对象，然后对就诊人的相关信息进行注入
        patientList.stream().forEach(item->{
            this.packageParm(item);
        });

        return  patientList;
    }

    //Service层：根据就诊人的编号获取具体的就诊人
    @Override
    public Patient getPatientById(Long id) {
        Patient patient = patientMapper.selectById(id);
        //对就诊人相关信息的注入
        this.packageParm(patient);
        return  patient;
    }

    //对每一个就诊人的相关信息进行注入，例如就诊人身份证编号、数据字典（省市区地址）等
    private Patient packageParm(Patient patient) {
        //根据证件类型编码，获取证件类型具体指
        String certficaltesType = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        //联系人证件类型(联系人证件可有可无),首先判断是否有具体的证件类型编号
        if (StringUtils.isEmpty(patient.getContactsCertificatesType())) {
            patient.getParam().put("contactsCertificatesTypeString", "");
        } else {
            String contactsCertificatesType = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
            patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesType);
        }
        //省
        String province = dictFeignClient.getNameByValue(patient.getProvinceCode());
        //市
        String city = dictFeignClient.getNameByValue(patient.getCityCode());
        //区
        String district = dictFeignClient.getNameByValue(patient.getDistrictCode());
        // 参数封装
        patient.getParam().put("certificatesTypeString", certficaltesType);
        patient.getParam().put("provinceString", province);
        patient.getParam().put("cityString", city);
        patient.getParam().put("districtString", district);
        patient.getParam().put("fullAddress",  province + city +  district + patient.getAddress());
        return patient;

    }
}
